package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class XiNing extends City {

	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int idx=body.indexOf("共查询到【 ");
				if (idx>0){
					String totalstr=body.substring(idx+6).split(" 】条违章信息")[0].trim();
		            int total=Integer.parseInt(totalstr);
		            if (total>0){
		            	String str=body;
		                str=str.split("违章地点")[1];
		                str=str.split("</table>")[0];
		                String[] rows = str.split("<tr");
		                boolean firstrow = true;
		                String retvalue = "";
		                for (int i=0;i<rows.length;i++){
		                    if (firstrow){
		                        firstrow=false;
		                        continue;
		                    }
		                    String row=rows[i];
		                    String[] cols = row.split("<td");
		                    String shijian = cols[3].split(">")[2].split("<")[0];
		                    String didian = cols[4].split(">")[2].split("<")[0];
		                    String code = cols[2].split(">")[2].split("<")[0];
		                    retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
		                    callback.onComplete(total, retvalue);
		                    return;
		            }
				}
				}
                callback.onComplete(0, "");
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,request1handler,"GBK").post("http://220.167.137.10/xh_traffic/traff_inv_qry.php", 
					String.format("car_type=02&ems_qry_input=%s",URLEncoder.encode(chepai,"GBK")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("青A");
	}

	@Override
	public String[] paramDesc() {
		return null;
	}

}
